package com.yntx.usercenter.authentication.controller;

import com.yntx.usercenter.authentication.dto.JwtPayloadDto;
import com.yntx.usercenter.authentication.service.TokenService;
import com.yntx.usercenter.common.web.ApiResult;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 令牌控制器
 *
 * @author jielihaofeng
 * @date 2023-04-26 14:21
 */
@RestController
@RequestMapping("/token")
@AllArgsConstructor
public class TokenController {

    private TokenService tokenService;

    /**
     * 生成令牌
     *
     * @param jwtPayloadDto jwt载荷dto
     * @return {@link ApiResult}<{@link String}>
     * @throws Exception 异常
     */
    @PostMapping("/generateToken")
    public ApiResult<String> generateToken(@RequestBody JwtPayloadDto jwtPayloadDto) throws Exception {
        return ApiResult.ofSuccess(tokenService.generateToken(jwtPayloadDto));
    }

    /**
     * 验证令牌，主要是解析token
     * 目前是采用redis有状态的玩法
     *
     * @param token 令牌
     * @return {@link ApiResult}<{@link JwtPayloadDto}>
     * @throws Exception 异常
     */
    @GetMapping("/verifyToken")
    public ApiResult<JwtPayloadDto> verifyToken(@RequestParam("token") String token) throws Exception {
        return ApiResult.ofSuccess(tokenService.verifyToken(token));
    }

}
